﻿using Anima2D;
using LostPolygon.DynamicWaterSystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FishManager : MonoBehaviour
{
    public List<GameObject> fishPre;
    public Transform from;
    public Transform to;
    public int fishNum;
    public BBInputController inputCtrl;


    public DynamicWater Water = null;
    public float SplashForce = 10f;
    public float SplashRadius = 0.25f;
    public Camera Camera;

    public List<Vector3> lastWaterPoints = new List<Vector3>();

    private void Start()
    {
        inputCtrl = BBIPhoneTouchManager.instance.inputController();

    }

    // Update is called once per frame
    void Update()
    {
        GetWater();
        CheckNum();
        CheckTouch();
    }

    public void CheckNum()
    {
        if (FishCtrl.fsList.Count < fishNum)
        {
            CreateFish();
        }
    }

    public void CreateFish()
    {
        //if (Random.Range(0, 100) < 50)
        //{
        //    Vector3 fromPos = new Vector3(from.position.x, from.position.y, Random.Range(from.position.z, to.position.z));
        //    Vector3 toPos = new Vector3(to.position.x, to.position.y, Random.Range(from.position.z, to.position.z));

        //    FishCtrl fc = Instantiate(fishPre[Random.Range(0, fishPre.Count)], fromPos, Quaternion.LookRotation(toPos - fromPos)).GetComponent<FishCtrl>();
        //    fc.transform.GetChild(0).GetComponent<Canvas>().sortingOrder = FishCtrl.fsList.Count * 2 - 3000;
        //    fc.transform.GetChild(1).GetChild(0).GetComponent<Canvas>().sortingOrder = FishCtrl.fsList.Count * 2 - 3001;
        //    fc.fm = this;
        //    FishCtrl.fsList.Add(fc);
        //}
        //else
        //{
        //    Vector3 fromPos = new Vector3(to.position.x, to.position.y, Random.Range(from.position.z, to.position.z));
        //    Vector3 toPos = new Vector3(from.position.x, from.position.y, Random.Range(from.position.z, to.position.z));

        //    FishCtrl fc = Instantiate(fishPre[Random.Range(0, fishPre.Count)], fromPos, Quaternion.LookRotation(toPos - fromPos)).GetComponent<FishCtrl>();

        //    fc.transform.GetChild(0).GetComponent<Canvas>().sortingOrder = FishCtrl.fsList.Count * 2 - 3000;
        //    fc.transform.GetChild(1).GetChild(0).GetComponent<Canvas>().sortingOrder = FishCtrl.fsList.Count * 2 - 3001;
        //    fc.fm = this;
        //    FishCtrl.fsList.Add(fc);
        //}


        Vector3 pos = new Vector3(Random.Range(from.position.x, to.position.x), from.position.y, Random.Range(from.position.z, to.position.z));
        Quaternion qua = Quaternion.LookRotation(new Vector3(Random.Range(-1, 1), 0, Random.Range(-1, 1)));
        FishCtrl fc = Instantiate(fishPre[Random.Range(0, fishPre.Count)], pos, qua).GetComponent<FishCtrl>();
        fc.transform.GetChild(0).GetComponent<SpriteMeshInstance>().sortingOrder = FishCtrl.fsList.Count;
        fc.fm = this;
        FishCtrl.fsList.Add(fc);
    }

    public void CheckTouch()
    {
        if (!inputCtrl.isConnected())
        {
            Debug.Log("连接失败");
            return;
        }
        List<Vector3> touchList = new List<Vector3>();
        for (int i = 0; i < iPhoneInput.touchCount; i++)
        {
            Ray ray = Camera.main.ScreenPointToRay(iPhoneInput.GetTouch(i).position);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform != null)
                {

                    bool isHas = false;
                    foreach (var t in lastWaterPoints)
                    {
                        if (Vector3.Distance(t, new Vector3(hit.point.x, from.position.y, hit.point.z)) < 0.01f)
                        {
                            isHas = true;
                            break;
                        }
                    }
                    if (!isHas)
                    {
                        if (iPhoneInput.GetTouch(i).phase == iPhoneTouchPhase.Began)
                        {
                            for (int k = 0; k < 10; k++)
                            {
                                Vector3 dv = new Vector3(Random.Range(-0.7f, 0.7f), 0, Random.Range(-0.7f, 0.7f));
                                CheckWater(hit.point + dv, 0.2f, 20f);
                            }

                        }
                        else
                        {
                            CheckWater(hit.point, SplashRadius, SplashForce);
                        }
                    }

                    touchList.Add(new Vector3(hit.point.x, from.position.y, hit.point.z));
                }
            }
        }
        lastWaterPoints = touchList;
        if (touchList.Count > 0)
        {
            FishCtrl.touches = touchList;
            foreach (var t in FishCtrl.fsList)
            {
                t.Check();
            }
        }
    }

    public void GetWater()
    {
        if (Water == null)
        {
            return;
        }

        if (Camera == null)
        {
            try
            {
                Camera = Camera.main ?? GetComponent<Camera>();
            }
            catch { }

            if (Camera == null)
            {
                Debug.LogError("No Camera attached and no active Camera was found, please set the Camera property for DW_MouseSplash to work", this);
                gameObject.SetActive(false);
                return;
            }
        }
    }

    public void CheckWater(Vector3 pos, float r, float f)
    {
        if (Water != null && pos != Vector3.zero)
        {
            print("波纹");
            Debug.DrawLine(Camera.transform.position, pos, Color.red);
            Water.GetWaterLevel(pos.x, pos.y, pos.z);
            Water.CreateSplash(pos, pos, r, -f * Time.deltaTime);

        }
    }
}
